package cn.org.itool.common.factory;

import cn.hutool.core.collection.CollectionUtil;
import cn.org.itool.common.constent.CodeEnum;
import cn.org.itool.common.constent.LoginEnum;
import cn.org.itool.common.exception.BizException;
import cn.org.itool.common.service.LoginService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

@Component
public class FrameLoginServiceFactory {

    @Resource
    private List<LoginService> loginServiceList;

    public String getUserIdByToken(String token, LoginEnum loginEnum) {
        LoginService loginService = this.getInstance(loginEnum);
        if(Objects.isNull(loginService)){
            throw new BizException(CodeEnum.ERROR, "LoginService#getUserIdByToken impl is null");
        }

        return loginService.getUserIdByToken(token);
    }

    private LoginService getInstance(LoginEnum loginEnum){
        if(CollectionUtil.isEmpty(this.loginServiceList)){
            return null;
        }
        for (LoginService loginService : loginServiceList) {
            if(loginService.type().getCode().equals(loginEnum.getCode())){
                return loginService;
            }
        }
        return null;
    }
}
